package com.smfx.yxt.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.daoSqlserver.AttachmentListMapper;
import com.smfx.yxt.daoSqlserver.StudentRoutineMapper;
import com.smfx.yxt.model.AttachmentList;
import com.smfx.yxt.model.StudentRoutine;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.AttachRelationReqVO;
import com.smfx.yxt.reqVo.StudentRoutineReqVO;
import com.smfx.yxt.service.IClassPointService;
import com.smfx.yxt.service.IStudentPointService;
import com.smfx.yxt.service.IStudentRoutineService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.view.StudentRoutineView;

@Service("studentRoutineService")
public class StudentRoutineServiceImpl implements IStudentRoutineService {

	@Autowired
	private StudentRoutineMapper studentRoutineMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IStudentPointService studentPointService;
	@Autowired
	private IClassPointService classPointService;
	
	@Override
	public List<StudentRoutineView> selectListByPage(Page<StudentRoutineView> page) {
		return studentRoutineMapper.selectListByPage(page);
	}

	@Override
	public StudentRoutineView selectViewById(Integer id) {
		StudentRoutineView view = studentRoutineMapper.selectViewById(id);
		if(view.getAttachRelationId() != null) {
			Map<String, Object> map = new HashMap<>();
			map.put("type", RelationType.STUDENT_ROUTINE.getCode());
			map.put("objId", view.getAttachRelationId());
			List<AttachmentList> attachmentList = attachmentListMapper.queryAttachmentListByRelationId(map);
			view.setAttachList(attachmentList);
		}
		return view;
	}

	@Override
	public void addStudentRoutine(StudentRoutineReqVO vo) {
		if(vo.getAttachIds()!= null) {
			AttachRelationReqVO attachReqVO = new AttachRelationReqVO();
			String[] array = vo.getAttachIds().split(",");
			Long relationId = Long.valueOf(array[0]);
			vo.setAttachRelationId(relationId);
			attachReqVO.setRelationId(relationId);
			attachReqVO.setAttachIds(array);
			attachmentListMapper.updateRelationIdByIds(attachReqVO);
		}
		if(vo.getScoreType() == 0) {
			vo.setScore(0 - vo.getScore());
		}
		vo.setCreateTime(new Date());
		studentRoutineMapper.addStudentRoutine(vo);
		new Thread(new StudentRoutineExecutor(vo)).start();
	}

	@Override
	public int updateByPrimaryKeySelective(StudentRoutine record) {
		return studentRoutineMapper.updateByPrimaryKeySelective(record);
	}

	class StudentRoutineExecutor implements Runnable {
		
		private StudentRoutineReqVO vo;
		
		public StudentRoutineExecutor(StudentRoutineReqVO vo) {
			this.vo = vo;
		}

		@Override
		public void run() {
			try { 
				String[] array = vo.getTargetIds();
				for(String key : array) {
					if(vo.getTargetType() == 1) {
						classPointService.updateClassMonthPoints(
								Integer.valueOf(key), vo.getScore());
					}else if(vo.getTargetType() == 2) {
							studentPointService.updateStudentMonthPoints(
									Integer.valueOf(key), vo.getScore());
					}
				}
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        }
		}
	}

}
